import { LRUCache } from 'lru-cache';
import { Injectable } from '@nestjs/common';
import { AllConfigType } from '@/config/config.type';
import { ConfigService } from '@nestjs/config';
import ms from 'ms';
@Injectable()
export class CacheService {
  cache: LRUCache<any, any>;
  constructor(private configService: ConfigService<AllConfigType>) {
    const expires = this.configService.getOrThrow('auth.expires', {
      infer: true,
    });
    this.cache = new LRUCache({
      max: 0, // 缓存最大项数
      ttl: ms(expires), // 缓存项最大存活时间（1小时）
      ttlAutopurge: true,
    });
    console.log('===========初始化CacheService================');
  }
  setItem(name: string, val: any) {
    this.cache.set(name, val);
  }
  getItem(name: string) {
    return this.cache.get(name);
  }
  getCache() {
    return this.cache;
  }
  delItem(name: string) {
    return this.cache.delete(name);
  }
  hasItem(name: string) {
    return this.cache.has(name);
  }
  clearItem() {
    return this.cache.clear();
  }
}
